unit fMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, IniFiles, Math, ComCtrls, Mask, Menus,
  uKeyboardMouse, uConstants, ImgList;

type
  TMain = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    rgNick: TRadioGroup;
    bRun: TButton;
    Button1: TButton;
    lx: TLabel;
    ly: TLabel;
    lr: TLabel;
    lg: TLabel;
    lb: TLabel;
    edx: TEdit;
    edy: TEdit;
    edr: TEdit;
    edg: TEdit;
    edb: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    PageControl1: TPageControl;
    tsImage: TTabSheet;
    Image1: TImage;
    tsConfig: TTabSheet;
    gbCommonConfig: TGroupBox;
    gbClassConfig: TGroupBox;
    ilIcons: TImageList;
    Label6: TLabel;
    Image2: TImage;
    ComboBox1: TComboBox;
    Label7: TLabel;
    Image3: TImage;
    ComboBox2: TComboBox;
    Label8: TLabel;
    Image4: TImage;
    ComboBox3: TComboBox;
    Label9: TLabel;
    Image5: TImage;
    ComboBox4: TComboBox;
    Label10: TLabel;
    Image6: TImage;
    ComboBox5: TComboBox;
    Label11: TLabel;
    Image7: TImage;
    ComboBox6: TComboBox;
    Label12: TLabel;
    Image8: TImage;
    ComboBox7: TComboBox;
    Label13: TLabel;
    Image9: TImage;
    ComboBox8: TComboBox;
    Label14: TLabel;
    Image10: TImage;
    ComboBox9: TComboBox;
    Label15: TLabel;
    Image11: TImage;
    ComboBox10: TComboBox;
    Label16: TLabel;
    Image12: TImage;
    ComboBox11: TComboBox;
    Label17: TLabel;
    Image13: TImage;
    ComboBox12: TComboBox;
    Label18: TLabel;
    Image14: TImage;
    ComboBox13: TComboBox;
    Label19: TLabel;
    Image15: TImage;
    ComboBox14: TComboBox;
    Label20: TLabel;
    Image16: TImage;
    ComboBox15: TComboBox;
    Label21: TLabel;
    Image17: TImage;
    ComboBox16: TComboBox;
    Label22: TLabel;
    Image18: TImage;
    ComboBox17: TComboBox;
    Label23: TLabel;
    Image19: TImage;
    ComboBox18: TComboBox;
    Label24: TLabel;
    Image20: TImage;
    ComboBox19: TComboBox;
    Label25: TLabel;
    Image21: TImage;
    ComboBox20: TComboBox;
    Label26: TLabel;
    Image22: TImage;
    ComboBox21: TComboBox;
    Label27: TLabel;
    Image23: TImage;
    ComboBox22: TComboBox;
    Label28: TLabel;
    Image24: TImage;
    ComboBox23: TComboBox;
    Label29: TLabel;
    Image25: TImage;
    ComboBox24: TComboBox;
    cbTest: TCheckBox;
    TabSheet1: TTabSheet;
    GroupBox1: TGroupBox;
    cbPotions: TCheckBox;
    cbRes: TCheckBox;
    cbSearch: TCheckBox;
    cbScript: TCheckBox;
    cbPvP: TCheckBox;
    procedure bRunClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image1Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure rgNickClick(Sender: TObject);
    procedure cbPvPClick(Sender: TObject);
    procedure cbPotionsClick(Sender: TObject);
  private
    FConfig: TIniFile;
    SelectedClass: byte;
    procedure LoadClassConfig(ClassID: byte);
    procedure SetSpoilerConfig;
    procedure SetOverConfig;
    procedure SetSourcerConfig;
    procedure SetSpellHowlerConfig;
    procedure SetSpellSingerConfig;
    procedure SetBladeDancerConfig;
    procedure SetSwordSingerConfig;
    procedure SetElementalSummonerConfig;
    procedure LoadSkillIcon(SkillID: integer);
    procedure LoadConfig;
  public
    { Public declarations }
  end;

var
  Main: TMain;

implementation

uses
    uMainData, uScreenController;
{$R *.dfm}


function HexToInt(s: string): integer;
var
  i: integer;
begin
    Result := 0;
    s := UpperCase(s);
    for i := Length(s) downto 1 do
    begin
        case s[i] of
                '0','1','2','3','4',
                '5','6','7','8','9':
                Result := Result + (StrToInt (s[i])*Round(IntPower(16,Length(s)-i)));
                'A': Result := Result + 10*Round(IntPower(16,Length(s)-i));
                'B': Result := Result + 11*Round(IntPower(16,Length(s)-i));
                'C': Result := Result + 12*Round(IntPower(16,Length(s)-i));
                'D': Result := Result + 13*Round(IntPower(16,Length(s)-i));
                'E': Result := Result + 14*Round(IntPower(16,Length(s)-i));
                'F': Result := Result + 15*Round(IntPower(16,Length(s)-i));
            else
            begin
                Result := 0;
                exit;
            end;
        end;
    end;
end;



procedure TMain.bRunClick(Sender: TObject);
begin
    MainData.StartScan;
end;

procedure TMain.Button1Click(Sender: TObject);
begin
    MainData.StopScan;
end;

procedure TMain.cbPotionsClick(Sender: TObject);
begin
    if TCheckBox(Sender).Checked then
        cbPvP.Checked :=  not TCheckBox(Sender).Checked;
end;

procedure TMain.cbPvPClick(Sender: TObject);
begin
    if TCheckBox(Sender).Checked then
        cbPotions.Checked := not TCheckBox(Sender).Checked;
end;

procedure TMain.ComboBox1Change(Sender: TObject);
begin
    FConfig.WriteInteger(IntToStr(SelectedClass),TComboBox(Sender).Name,TComboBox(Sender).ItemIndex);
end;

function RGBToColor(R,G,B:Byte): TColor;
begin
  Result:=B Shl 16 Or
          G Shl 8  Or 
          R;
end;

procedure TMain.FormShow(Sender: TObject);
begin
    FConfig := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'config.cfg');
    Image1.Picture.Bitmap.Width := Screen.Width;
    Image1.Picture.Bitmap.Height := Screen.Height;
    SelectedClass := 9;
    rgNick.ItemIndex := SelectedClass;
    LoadClassConfig(SelectedClass);
    LoadConfig;
end;

procedure TMain.Image1Click(Sender: TObject);
begin
    edr.Text := lr.Caption;
    edg.Text := lg.Caption;
    edb.Text := lb.Caption;
    edx.Text := lx.Caption;
    edy.Text := ly.Caption;
end;

procedure TMain.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
    C: integer;
begin
    C := ColorToRGB(Image1.Picture.Bitmap.Canvas.Pixels[X,Y]);
    lr.Caption := IntToStr(GetRValue(C));
    lg.Caption := IntToStr(GetGValue(C));
    lb.Caption := IntToStr(GetBValue(C));
    lx.Caption := IntToStr(x);
    ly.Caption := IntToStr(y);
end;


procedure TMain.rgNickClick(Sender: TObject);
begin
    if TRadioGroup(Sender).ItemIndex <> SelectedClass then
    begin
        SelectedClass := TRadioGroup(Sender).ItemIndex;
        LoadClassConfig (SelectedClass);
    end;
end;

procedure TMain.LoadClassConfig(ClassID: byte);
begin
    case ClassID of
        0: SetOverConfig;
        1: SetElementalSummonerConfig;
        2: SetSourcerConfig;
        4: SetSpellHowlerConfig;
        5: SetSwordSingerConfig;
        6: SetBladeDancerConfig;
        8: SetSpellSingerConfig;
        9: SetSpoilerConfig;
    end;
end;

procedure TMain.SetSpoilerConfig;
var
    i: integer;
begin
    for i := 0 to gbClassConfig.ControlCount - 1 do
    begin
        if gbClassConfig.Controls[i] is TImage then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: ilIcons.GetBitmap(imgSpoil,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                1: ilIcons.GetBitmap(imgSweep,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                else TImage(gbClassConfig.Controls[i]).Picture := nil;
            end;
            TImage(gbClassConfig.Controls[i]).Refresh;
        end;
        if gbClassConfig.Controls[i] is TLabel then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: TLabel(gbClassConfig.Controls[i]).Caption := 'Spoil';
                1: TLabel(gbClassConfig.Controls[i]).Caption := 'Sweeper';
                else TLabel(gbClassConfig.Controls[i]).Caption := '';
            end;
        end;
    end;
end;

procedure TMain.SetOverConfig;
var
    i: integer;
begin
    for i := 0 to gbClassConfig.ControlCount - 1 do
    begin
        if gbClassConfig.Controls[i] is TImage then
        begin

            case gbClassConfig.Controls[i].Tag of
                0: ilIcons.GetBitmap(imgStealEssence,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                1: ilIcons.GetBitmap(imgHonorofPaagrio,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                2: ilIcons.GetBitmap(imgRitualofLife,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                3: ilIcons.GetBitmap(imgVictoryOfPaagrio,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                4: ilIcons.GetBitmap(imgPaagriosFist,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                5: ilIcons.GetBitmap(imgPaagriosEmblem,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                6: ilIcons.GetBitmap(imgBaffMacro,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                else TImage(gbClassConfig.Controls[i]).Picture := nil;
            end;
            TImage(gbClassConfig.Controls[i]).Refresh;
        end;
        if gbClassConfig.Controls[i] is TLabel then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: TLabel(gbClassConfig.Controls[i]).Caption := 'DrainHP';
                1: TLabel(gbClassConfig.Controls[i]).Caption := 'ClanCP';
                2: TLabel(gbClassConfig.Controls[i]).Caption := 'TargetCP';
                3: TLabel(gbClassConfig.Controls[i]).Caption := 'VoP';
                4: TLabel(gbClassConfig.Controls[i]).Caption := 'BlessCP';
                5: TLabel(gbClassConfig.Controls[i]).Caption := 'ResistDebuff';
                6: TLabel(gbClassConfig.Controls[i]).Caption := 'BaffMacro';
                else TLabel(gbClassConfig.Controls[i]).Caption := '';
            end;
        end;
    end;
end;

procedure TMain.LoadSkillIcon(SkillID: integer);
begin

end;

procedure TMain.LoadConfig;
var
    i: integer;
    CB: TComboBox;
begin
    for i := 1 to 24 do
    begin
        CB := TComboBox(FindComponent('ComboBox' + IntToStr(i)));
        CB.ItemIndex := FConfig.ReadInteger(IntToStr(SelectedClass),'ComboBox' + IntToStr(i),-1);
    end;
end;

procedure TMain.SetSourcerConfig;
var
    i: integer;
begin
    for i := 0 to gbClassConfig.ControlCount - 1 do
    begin
        if gbClassConfig.Controls[i] is TImage then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: ilIcons.GetBitmap(imgProminance,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                1: ilIcons.GetBitmap(imgAssist,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                else TImage(gbClassConfig.Controls[i]).Picture := nil;
            end;
            TImage(gbClassConfig.Controls[i]).Refresh;
        end;
        if gbClassConfig.Controls[i] is TLabel then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: TLabel(gbClassConfig.Controls[i]).Caption := 'Prominance';
                1: TLabel(gbClassConfig.Controls[i]).Caption := 'Assist';
                else TLabel(gbClassConfig.Controls[i]).Caption := '';
            end;
        end;
    end;
end;

procedure TMain.SetSpellHowlerConfig;
var
    i: integer;
begin
    for i := 0 to gbClassConfig.ControlCount - 1 do
    begin
        if gbClassConfig.Controls[i] is TImage then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: ilIcons.GetBitmap(imgHurricane,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                1: ilIcons.GetBitmap(imgAssist,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                else TImage(gbClassConfig.Controls[i]).Picture := nil;
            end;
            TImage(gbClassConfig.Controls[i]).Refresh;
        end;
        if gbClassConfig.Controls[i] is TLabel then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: TLabel(gbClassConfig.Controls[i]).Caption := 'Hurricane';
                1: TLabel(gbClassConfig.Controls[i]).Caption := 'Assist';
                else TLabel(gbClassConfig.Controls[i]).Caption := '';
            end;
        end;
    end;
end;

procedure TMain.SetSpellSingerConfig;
var
    i: integer;
begin
    for i := 0 to gbClassConfig.ControlCount - 1 do
    begin
        if gbClassConfig.Controls[i] is TImage then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: ilIcons.GetBitmap(imgHydroBlast,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                1: ilIcons.GetBitmap(imgAssist,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                else TImage(gbClassConfig.Controls[i]).Picture := nil;
            end;
            TImage(gbClassConfig.Controls[i]).Refresh;
        end;
        if gbClassConfig.Controls[i] is TLabel then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: TLabel(gbClassConfig.Controls[i]).Caption := 'HydroBlast';
                1: TLabel(gbClassConfig.Controls[i]).Caption := 'Assist';
                else TLabel(gbClassConfig.Controls[i]).Caption := '';
            end;
        end;
    end;
end;

procedure TMain.SetBladeDancerConfig;
var
    i: integer;
begin
    for i := 0 to gbClassConfig.ControlCount - 1 do
    begin
        if gbClassConfig.Controls[i] is TImage then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: ilIcons.GetBitmap(imgDanceMystic,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                1: ilIcons.GetBitmap(imgDanceofConcentration,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                2: ilIcons.GetBitmap(imgSirenDance,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                else TImage(gbClassConfig.Controls[i]).Picture := nil;
            end;
            TImage(gbClassConfig.Controls[i]).Refresh;
        end;
        if gbClassConfig.Controls[i] is TLabel then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: TLabel(gbClassConfig.Controls[i]).Caption := 'DanceMystic';
                1: TLabel(gbClassConfig.Controls[i]).Caption := 'Concentration';
                2: TLabel(gbClassConfig.Controls[i]).Caption := 'Siren''sDance';
                else TLabel(gbClassConfig.Controls[i]).Caption := '';
            end;
        end;
    end;
end;

procedure TMain.SetSwordSingerConfig;
var
    i: integer;
begin
    for i := 0 to gbClassConfig.ControlCount - 1 do
    begin
        if gbClassConfig.Controls[i] is TImage then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: ilIcons.GetBitmap(imgSongofEarth,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                1: ilIcons.GetBitmap(imgSongofVitality,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                2: ilIcons.GetBitmap(imgSongofRenewal,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                3: ilIcons.GetBitmap(imgSongofMeditation,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                else TImage(gbClassConfig.Controls[i]).Picture := nil;
            end;
            TImage(gbClassConfig.Controls[i]).Refresh;
        end;
        if gbClassConfig.Controls[i] is TLabel then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: TLabel(gbClassConfig.Controls[i]).Caption := 'Vitality';
                1: TLabel(gbClassConfig.Controls[i]).Caption := 'Earth';
                2: TLabel(gbClassConfig.Controls[i]).Caption := 'Renewal';
                3: TLabel(gbClassConfig.Controls[i]).Caption := 'Meditation';
                else TLabel(gbClassConfig.Controls[i]).Caption := '';
            end;
        end;
    end;
end;

procedure TMain.SetElementalSummonerConfig;
var
    i: integer;
begin
    for i := 0 to gbClassConfig.ControlCount - 1 do
    begin
        if gbClassConfig.Controls[i] is TImage then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: ilIcons.GetBitmap(imgGiftofSeraphim,TImage(gbClassConfig.Controls[i]).Picture.Bitmap);
                else TImage(gbClassConfig.Controls[i]).Picture := nil;
            end;
            TImage(gbClassConfig.Controls[i]).Refresh;
        end;
        if gbClassConfig.Controls[i] is TLabel then
        begin
            case gbClassConfig.Controls[i].Tag of
                0: TLabel(gbClassConfig.Controls[i]).Caption := 'GiftofSeraphim';
                else TLabel(gbClassConfig.Controls[i]).Caption := '';
            end;
        end;
    end;
end;


end.
